package com.example.tests;

import static com.example.tests.ContactsDataGenerator.loadContactsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactsCreationTests extends TestBase
{
	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException
	{
		return wrapContactsDataForDataProvider(loadContactsFromXmlFile(new File("contacts.xml"))).iterator();
	}

	@Test(dataProvider = "randomValidContactGenerator")
	public void testContactCreation(ContactData contact) throws Exception
	{
		SortedListOf<ContactData> oldList = app.getModel().getContacts();

		app.getContactHelper().createContact(contact);

		SortedListOf<ContactData> newList = app.getModel().getContacts();

		assertThat(newList, equalTo(oldList.withAdded(contact)));

		if (timeToCheck())
		{
			if ("yes".equals(app.getProperty("check.db")))
			{
				assertThat(newList, equalTo(app.getHibernateHelper().getContacts()));
			}
			if ("yes".equals(app.getProperty("check.ui")))
			{
				assertThat(newList, equalTo(app.getContactHelper().getUIContacts()));
			}
		}
	}
}
